package org.electrical.controller.functionality;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.electrical.dao.EleCustomerDao;
import org.electrical.dao.EleLossDao;
import org.electrical.pojo.EleCustomer;
import org.electrical.pojo.EleLoss;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/loss")
@CrossOrigin(value = "*")
public class EleLossController {

    @Autowired
    private EleLossDao eleLossDao;
    @Autowired
    private EleCustomerDao eleCustomerDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleLoss>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, EleLoss condition) throws Exception {

        Page<EleLoss> pager = PageHelper.startPage(page, limit);
        List<EleLoss> list = eleLossDao.getConditionList(condition);
        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(EleLoss eleLoss) throws Exception {

        eleCustomerDao.early(eleLoss.getCustomerId());
        eleLossDao.add(eleLoss);
        return ResponseResult.createSuccessResult();

    }

    /**
     * 暂缓流失
     */
    @RequestMapping("/measures")
    @ResponseBody
    public ResponseResult measures(EleLoss eleLoss) throws Exception {

        eleCustomerDao.measures(eleLoss.getCustomerId());
        eleLossDao.measures(eleLoss);
        return ResponseResult.createSuccessResult();

    }

    /**
     * 确认流失
     */
    @RequestMapping("/reason")
    @ResponseBody
    public ResponseResult reason(EleLoss eleLoss) throws Exception {

        eleCustomerDao.reason(eleLoss.getCustomerId());
        eleLossDao.reason(eleLoss);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/cusLoss")
    @ResponseBody
    public ResponseResult<List<EleLoss>> lossCus(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, EleLoss condition) throws Exception {

        Page<EleLoss> pager = PageHelper.startPage(page, limit);
        List<EleLoss> list = eleLossDao.getLoss(condition);
        return new ResponseResult(pager.getTotal(), list);

    }

}
